輝度調整

印刷の時、明るさなど調整したい事があり、「DOBU.NET」のサンプルを参考にして輝度調整のプログラムを作ってみました。

左図は実行時の状態で、FormにPictureBox1,2、hScrollBar1など配置、右のPictureBox2に元画像、左に修正画像を表示
明るさはhScrollBar1のつまみ、矢印をうごかして調節して見ました

OpenFileDialogを利用し画像を選択できるようにしました
時に、Dialog起動が遅いことがあり、Propatyの赤丸の所を変更しました。
PictureBoxのSizeModeはZoomとしてあります
using System.Reflection.Emit;
using System.Windows.Forms;

namespace kido
{
    public partial class Form1 : Form
    {
        Bitmap newImg;
        int Vol;
        public Form1()
        {
            InitializeComponent();
        }
        //***************************************************************************
        public static System.Drawing.Image
            AdjustBrightness(System.Drawing.Image img, int brightness, Bitmap newImg)
        {
            //Bitmap newImg = new Bitmap(img.Width, img.Height);

            Graphics g = Graphics.FromImage(newImg);
            float plusVal = (float)brightness / 255f;

            System.Drawing.Imaging.ColorMatrix cm =
                                       new System.Drawing.Imaging.ColorMatrix(
                                   new float[][] {
                                   new float[] {1.05f, 0, 0, 0, 0},
                                   new float[] {0, 1.05f, 0, 0, 0},
                                   new float[] {0, 0, 1.05f, 0, 0},
                                   new float[] {0, 0, 0, 1, 0},
                                   new float[] {plusVal, plusVal, plusVal, 0, 1}
                                });

            System.Drawing.Imaging.ImageAttributes ia =
                                    new System.Drawing.Imaging.ImageAttributes();
            ia.SetColorMatrix(cm);
            g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height),
                                 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);

            g.Dispose();

            return newImg;
        }
        //*****************************************************************************
        private void hScrollBar1_ValueChanged(object sender, EventArgs e)
        {
            if (newImg != null)  // これがないとメモリ−不足が起こる
            {
                newImg.Dispose();
            }

            Vol = hScrollBar1.Value;
            label3.Text = Vol.ToString();

            newImg = new Bitmap(pictureBox2.Image.Width, pictureBox2.Image.Height);
            pictureBox1.Image = AdjustBrightness(pictureBox2.Image, Vol, newImg);            
        }       

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e) //FileOpen
        {
            String Fname;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Fname = openFileDialog1.FileName;
                label1.Text = Fname;

                Bitmap BMP = new Bitmap(Fname);
                pictureBox2.Image = BMP;
                pictureBox1.Image = BMP;

                hScrollBar1.Enabled = true;
                button1.Enabled = true;
            }
        }
    }
}


左図はソース プログラムです

AdjustBrightnessでBitmap newImg = new Bitmap(img.Width, img.Height);を作ると、メモリ−使用が輝度調整 するたびに増え、newImg.Dispose()でメモリ−消費を抑えたいのですが上手く行かず、newImgをグロ−バル変数として hScrollBar1_ValueChanged()でnewImg.Dispose()を行いました。

using(newImg = new Bitmap(pictureBox2.Image.Width, pictureBox2.Image.Height))で処理を試みりましたが エラ−が出て上手く処理できませんでした。